package com.hkyl.hospital.service.impl;

import com.hkyl.hospital.entity.*;
import com.hkyl.hospital.mapper.BillMapper;
import com.hkyl.hospital.mapper.MedicineMapper;
import com.hkyl.hospital.mapper.OrdersMapper;
import com.hkyl.hospital.mapper.PatientMedicineMapper;
import com.hkyl.hospital.service.PatientMedicineService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * @author hyh
 * @date 2023/07/15 16:29:26
 */
@Service
@Transactional
public class PatientMedicineServiceImpl implements PatientMedicineService {

    @Autowired
    private PatientMedicineMapper patientMedicineMapper;
    @Autowired
    private OrdersMapper ordersMapper;
    @Autowired
    private BillMapper billMapper;
    @Autowired
    private MedicineMapper medicineMapper;

    @Override
    public Integer save(PatientMedicine patientMedicine) {
        Integer patientId = patientMedicine.getPatientId();
        BigDecimal totalPrice = patientMedicine.getTotalPrice();
        Integer billId = addOrderAndUpdateBillCostPrice(patientId, "药物费", totalPrice);
        patientMedicine.setDate(new Date());
        patientMedicine.setBillId(billId);
        int rows = patientMedicineMapper.insertSelective(patientMedicine);
        Medicine medicine = medicineMapper.selectByPrimaryKey(patientMedicine.getMedicineId());
        medicine.setMedicineNum(medicine.getMedicineNum() - patientMedicine.getCount());
        rows += medicineMapper.updateByPrimaryKeySelective(medicine);
        return rows;
    }

    public Integer addOrderAndUpdateBillCostPrice(Integer patientId, String costName, BigDecimal costPrice) {
        //查询客户是否有order
        OrdersExample ordersExample = new OrdersExample();
        ordersExample.or().andPatientIdEqualTo(patientId).andIsDeleteEqualTo(0);
        List<Orders> orders = ordersMapper.selectByExample(ordersExample);
        if (orders.size() == 0){
            Orders order = new Orders();
            order.setOrderNo(getOrderNo());
            order.setPatientId(patientId);
            ordersMapper.insertSelective(order);
            orders.add(order);
        }
        //查询该order是否有药物bill
        String orderNo = orders.get(0).getOrderNo();
        BillExample billExample = new BillExample();
        billExample.or().andOrderNoEqualTo(orderNo).andCostNameEqualTo(costName);
        List<Bill> bills = billMapper.selectByExample(billExample);
        if (bills.size() == 0){
            Bill bill = new Bill();
            bill.setCostName(costName);
            bill.setCostPrice(costPrice);
            bill.setOrderNo(orderNo);
            billMapper.insertSelective(bill);
            bills.add(bill);
        }
        //更新costPrice
        Bill bill = bills.get(0);
        bill.setCostPrice(bill.getCostPrice().add(costPrice));
        billMapper.updateByPrimaryKeySelective(bill);
        return bill.getBillId();
    }

    private String getOrderNo(){
        String uuid = UUID.randomUUID().toString().replace("-","");
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd");
        String format = simpleDateFormat.format(new Date());
        return uuid + format;
    }
}
